constructive algorithms greedy implementation *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>indexed_set;


#pragma GCC optimize("Ofast")  
//#pragma GCC target("avx,avx2,fma") 
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("-O2")

#define f(i,a,b)        for(int i=(int)(a);i<=(int)(b);i++)
#define all(a)          (a).begin(), (a).end()
#define ll                 long long
#define fi                 first
#define se                 second
#define pb                 push_back
#define pob                pop_back
#define mem(ara,x)      memset((ara),(x),sizeof(ara))
#define memv(ara,x)     memset((&ara[0]),(x),sizeof(ara))
#define memn(ara)         memset((ara),-1,sizeof(ara))
#define bb                 begin()
#define ee                 end()
#define em                 emplace
#define eb                 emplace_back
#define D                 double
#define endl            '\n'

#define pii             pair<int,int>
#define pll             pair<long long int,long long int>
#define vi              vector<int>
#define vll             vector<long long int>
#define vii             vector<pii>
 
#define mx                 (1<<30)-1
#define llmx             (1ll<<62)

#define mod             1000000007
#define n_size             200005
#define g_size             20000005
#define eps             1e-9

#define sc1(a)             scanf("%d",&a)
#define sc2(a,b)         scanf("%d %d",&a,&b)
#define pf1(a)             printf("%d\n",a);
#define pf2(a,b)          printf("%d %d\n",a,b)

const double PI=acos(-1);

int Dx[]={-1,-1,0,1,1, 1, 0,-1};
int Dy[]={ 0, 1,1,1,0,-1,-1,-1};
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};

int arr[g_size];
vi v;

inline void solve(){

    

    int n,m,t,res;

    int l,r,mid;

    int maxi=-mx,mini=mx,count=0;

    int i,j,k;

    bool flag;

    int a,b;
    cin>>n>>a>>b;

    string s;
    cin>>s;

    for(i=0;i<n;i++){
        if(s[i]=='.') count++;
        else{
            if(count) v.pb(count);
            count=0;
        }
    }

    if(count) v.pb(count);
    count=0;


    sort(all(v));
    // for(auto x:v) cout<<x<<" ";
    // cout<<endl;
    if(a<b) swap(a,b);
    for(i=0;i<v.size();i++){
        if(a){
            if((v[i]+1)/2 >=a){
                count+=a;
                v[i]=v[i]-a;
                a=0;
            }
            else{
                a=a-((v[i]+1)/2);
                count+=((v[i]+1)/2);
                v[i]=v[i]-(v[i]+1)/2;
            }
        }
        else break;
    }

    // for(auto x:v) cout<<x<<" ";
    // cout<<endl;

    for(i=0;i<v.size();i++){
        if(v[i]==0) continue;
        if(b){
            if(v[i] >=b){
                count+=b;
                v[i]=v[i]-b;
                b=0;
            }
            else{
                b=b-v[i];
                count+=v[i];
                v[i]=0;
            }
        }
        else break;
    }

    cout<<count<<endl;
 

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    //freopen("input.in","r",stdin);
    //freopen("output.out","w",stdout);
    //cout<<fixed<<setprecision(11);


    int T=1;
    //cin>>T;

    while(T--) solve();
    
    
}


Comments

Submit
0 Comments
More Questions

733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences